/*
 * Created by: Mark Lavrynenko
 * Email:	MarkLavrinenko@gmail.com
 * Project:	  Huffman
*/

#include "FileDataSource.h"
#include <assert.h>


FileDataSource::FileDataSource(char const* fileName)
{
	f = fopen(fileName, "rb");
	assert(NULL != f);
	nextByte = fgetc(f);
}


FileDataSource::~FileDataSource(void)
{
	fclose(f);
}

uint8_t  FileDataSource::getNextByte()
{
	assert(EOF != nextByte);
	uint8_t res = nextByte;
	nextByte = fgetc(f);
	return res;
}

int16_t  FileDataSource::getNextInt16()
{
	uint8_t first = getNextByte();
	uint8_t second = getNextByte();
	return (int16_t)((first << 8) + second);
}

int32_t FileDataSource::getNextInt32()
{
	int32_t res = 0;
	for (size_t i = 0; i < 4; ++i)
	{
		uint8_t next = getNextByte();
		res = (res << 8)  + next;
	}
	return res;
}

bool FileDataSource::hasNextByte()
{
	return EOF != nextByte;
}

void FileDataSource::rewind()
{
	::rewind(f);
	nextByte = fgetc(f);
}